package com.tjf.skin_loading;

import static skin.support.widget.SkinCompatHelper.INVALID_ID;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;

import androidx.annotation.Nullable;

import skin.support.content.res.SkinCompatResources;
import skin.support.widget.SkinCompatHelper;
import skin.support.widget.SkinCompatView;

/**
 * Android自定义View绘制居中文本
 * 来源  可参考于https://www.jb51.net/article/253476.htm
 */
public class CustomTextView extends SkinCompatView {
    // paint 初始化
    private Paint mPaint = new Paint();
    private Rect mBound;
    int textColor;
    int textColorId;
    private String text = "";
    float textSize;

    public CustomTextView(Context context) {
        super(context);
    }

    public CustomTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public CustomTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        // 检索此上下文主题中的样式化属性信息
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.FindView);
        // 这里styleable 和面的跟的是attrs.xml中declare-styleable 里面的name和attr的name
        text = typedArray.getString(R.styleable.FindView_test_text);
        textColorId = typedArray.getResourceId(R.styleable.FindView_test_color,
                0);
        textSize = typedArray.getDimension(R.styleable.FindView_test_size, 33);
        setTextColor();
        mPaint.setTextSize(textSize);//画笔字体大小设置
        mPaint.setColor(textColor);//画笔的颜色
        mBound = new Rect();
        mPaint.getTextBounds(text, 0, text.length(), mBound);
        // 回收TypedArray，供以后的调用方重新使用, 这个不要忘记添加
        typedArray.recycle();

    }

    /**
     * EXACTLY：一般是设置了明确的值或者是MATCH_PARENT
     * AT_MOST：表示子布局限制在一个最大值内，一般为WARP_CONTENT
     * UNSPECIFIED：表示子布局想要多大就多大，很少使用
     *
     * @param widthMeasureSpec
     * @param heightMeasureSpec
     */

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        // 获取宽高的设置模式
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        //获取宽高的大小
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        //最终宽高
        int width;
        int height;
        if (widthMode == MeasureSpec.EXACTLY) {//当设定了宽度，测量的宽度就等于设定的宽度
            width = widthSize;
        } else {
            mPaint.setTextSize(textSize);
            mPaint.getTextBounds(text, 0, text.length(), mBound);
//            float textWidth = mBound.width();//这样宽度会不全，比系统的textView短
            float textWidth = mPaint.measureText(text);//比较精确的测量文本宽度的方式
            int desired = (int) (getPaddingLeft() + textWidth + getPaddingRight());
            width = desired;
        }

        if (heightMode == MeasureSpec.EXACTLY) {
            height = heightSize;
        } else {
            mPaint.setTextSize(textSize);
            mPaint.getTextBounds(text, 0, text.length(), mBound);
//            float textHeight = mBound.height();//这样高度会不全，比系统的textView窄
            Paint.FontMetrics fontMetrics = mPaint.getFontMetrics();
            float textHeight = Math.abs((fontMetrics.bottom - fontMetrics.top));

            int desired = (int) (getPaddingTop() + textHeight + getPaddingBottom());
            height = desired;
        }

        //最终设置宽高
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int startX = (int) (getWidth() / 2 - mPaint.measureText(text) / 2);
        //解决高度绘制不居中
        Paint.FontMetricsInt fm = mPaint.getFontMetricsInt();
        int startY = getHeight() / 2 - fm.descent + (fm.bottom - fm.top) / 2;
        canvas.drawText(text, startX, startY, mPaint);
    }

    public void setTextColor() {
        textColor = SkinCompatHelper.checkResourceId(textColorId);
        if (textColor != INVALID_ID) {
            textColor = SkinCompatResources.getColor(getContext(), textColorId);
        }
    }

    @Override
    public void applySkin() {
        setTextColor();
    }
}